<template>
  <div>
    <el-card class="box-card">
      <div slot="header">
        <my-bread> 评论管理</my-bread>
      </div>
      <template>
        <el-table :data="comments" style="width: 100%">
          <el-table-column prop="title" label="文章标题" width="180">
          </el-table-column>
          <el-table-column
            prop="total_comment_count"
            label="评论总数"
            width="180"
          >
          </el-table-column>
          <el-table-column prop="fans_comment_count" label="粉丝评论总数">
          </el-table-column>
          <el-table-column prop="address" label="评论状态">
            <template slot-scope="scope">
              {{ scope.row.comment_status ? "正常" : "关闭" }}
            </template>
          </el-table-column>
          <el-table-column prop="address" label="操作">
            <template slot-scope="scope">
              <el-button
                @click="SwitchState(scope.row)"
                v-if="scope.row.comment_status"
                type="success"
                >打开评论</el-button
              >
              <el-button @click="SwitchState(scope.row)" v-else type="danger"
                >关闭评论</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </template>
      <el-pagination
        :current-page="reqParams.page"
        :page-size="reqParams.per_page"
        @current-change="currentChange"
        background
        layout="prev, pager, next"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //    评论管理列表数据
      comments: [],
      // 总条数
      total: 0,
      //    参数对象
      reqParams: {
        page: 1,
        per_page: 20,
        response_type: "comment"
      }
    };
  },
  created() {
    this.getComment();
  },

  methods: {
    // 点击切换操作状态
    SwitchState(row) {
      const nxte1 = "你要打开评论操作";
      const nxte2 = "你要关闭评论操作";
      this.$confirm(row.comment_status ? nxte2 : nxte1, {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          try {
            //   点击操作
            const {
              data: { data }
              //   allow_comment: !row.comment_status  请求状态参数取反
            } = await this.$http.put(`/comments/status?article_id=${row.id}`, {
              allow_comment: !row.comment_status
            });
            console.log(data);
            // data.allow_comment; 获得请求状态  赋值当前行
            row.comment_status = data.allow_comment;
            // 刷新页面
            this.getComment();
          } catch (e) {
            this.$message.error("操作失败");
          }
        })
        .catch(() => {});
    },
    // 获取当前分页
    currentChange(number) {
      console.log(number);

      this.reqParams.page = number;
      this.getComment();
    },
    //   获取评论信息
    async getComment() {
      const {
        data: { data }
      } = await this.$http.get("/articles", { params: this.reqParams });
      console.log(data);
      //    赋值评论信息
      this.comments = data.results;
      //   获取总页数
      this.total = data.total_count;
    }
  }
};
</script>

<style></style>
